<?php

namespace app\admin\controller;


use app\admin\model\MemberModel;

class Member extends Base
{	
  public function index(){
      //获取全部用户
      $where['name']=input('name');
      if(input('name')){
          $member = MemberModel::where('name','like','%'.input('name').'%')->paginate(10);
      }else{
          $member= MemberModel::paginate(10);
      }
      $this->assign('member',$member);
      return $this->fetch();
  }
  //删除用户
    public function del(){
      if(request()->isAjax()){
          $id = input('id');
          if(!$id) return json(['code'=>1,'date'=>'','msg'=>'参数为空']);
          $re_del = MemberModel::destroy($id);
          if($re_del){
              return json(['code'=>200,'date'=>'','msg'=>'删除成功']);
          }else{
              return json(['code'=>2,'date'=>'','msg'=>'删除失败']);
          }
      }else{
          return json(['code'=>0,'date'=>'','msg'=>'非法请求']);
      }
    }
    //获取用户的详情
    public function get_member(){
      if(request()->isAjax()){
            if(!input('id')) return json(['code'=>0,'data'=>'','msg'=>'参数错误']);
            //查询用户
          $member = MemberModel::get(input('id'));
          if(!$member) return json(['code'=>1,'data'=>'','msg'=>'该用户不存在']);
          return json(['code'=>200,'data'=>$member,'msg'=>'ok']);
      }else{
          return json(['code'=>0,'date'=>'','msg'=>'非法请求']);
      }
    }
    //编辑
    public function edit(){
      if(request()->isPost()){
          $data  = input();
          if(empty($data['pwd'])){
              unset($data['pwd']);
          }
          $re = MemberModel::update_data($data);
          if($re){
              return $this->redirect('/admin/member');
          }else{
              return $this->error('修改失败(手机号或邮箱已存在)');
          }
      }else{
          $id = input('id');
         $member =  MemberModel::get($id);
         if(!$member) return $this->error('该用户不存在');
         $this->assign('member',$member);
          return $this->fetch();
      }
    }



}